package br.uvv.sao.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.uvv.sao.domain.OfertaDisciplina;
import br.uvv.sao.domain.Prova;
import br.uvv.sao.util.HibernateUtil;

public class RelatorioDAO extends DAO {

	private static final long serialVersionUID = 8280536125388966901L;
	
	@SuppressWarnings("unchecked")
	public List<OfertaDisciplina> listar(Integer idDisciplina, Integer idProfessor, String semestre, Date ano) {
		List<OfertaDisciplina> ofertaList = null;
		try {
			this.sessao = HibernateUtil.getSessionfactory().getCurrentSession();
			this.transacao = this.sessao.beginTransaction();
			Criteria filtro = this.sessao.createCriteria(OfertaDisciplina.class);
			if (idDisciplina > 0)
				filtro.add(Restrictions.eq("disciplina.id", idDisciplina));
			if (idProfessor > 0)
				filtro.add(Restrictions.eq("professor.id", idProfessor));
			if (!semestre.equals("0"))
				filtro.add(Restrictions.eq("semestre", semestre));
			//Falta a comparação de ano
			filtro.addOrder(Order.asc("id"));
			ofertaList = (ArrayList<OfertaDisciplina>) filtro.list();
			this.transacao.commit();
		} catch (Throwable e) {
			if (this.transacao.isActive()){
				this.transacao.rollback();
			}
		} finally {
			try {
				if (this.sessao.isOpen()){
					this.sessao.close();
				}
			} catch (Throwable e){
				System.out.println("ERRO - Relatorio/Listar: " + e.getMessage());
			}
		}
		return ofertaList;
	}

	@SuppressWarnings("unchecked")
	public List<Prova> listarProvaOferta(Long id) {
		List<Prova> provaList = null;
		try {
			this.sessao = HibernateUtil.getSessionfactory().getCurrentSession();
			this.transacao = this.sessao.beginTransaction();
			Criteria filtro = sessao.createCriteria(Prova.class);
			filtro.add(Restrictions.eq("", id));
			provaList = (ArrayList<Prova>) filtro.list();
			this.transacao.commit();
		} catch (Throwable e) {
			if (this.transacao.isActive()){
				this.transacao.rollback();
			}
		} finally {
			try {
				if (this.sessao.isOpen()){
					this.sessao.close();
				}
			} catch (Throwable e){
				System.out.println("ERRO - Relatorio/Listar: " + e.getMessage());
			}
		}
		return provaList;
	}
}
